define(function(require, exports, module) {
    "use strict";
    var $ = require("$");
    window.template = require("./template");
    var async = function(u, c, t, g, d) {
        $.ajax(u, {
            type: g || "post",
            data: d,
            dataType: "json"
        }).done(function(d) {
            c && c(d);
        }).fail(function() {
            window.msg.info(t || "网络异常 请重试")
        });
    }
    var method = {
        postd: function(u, c, d) {
            async(u, c, false, false, d)
        }
    }

    var shortcutMenu = function() {
        function loadMenu(obj) {
            var html = "",loadurl = obj== undefined||obj.loadUrl == undefined ? "/Plus/menu.php" : obj.loadUrl,
                select=[],data={},plist=[],clist=[];
            $("#shortcutHrefList a").each(function() {
               var url=$(this).data("url"),title=$(this).attr("title"),id=$(this).attr("id");
               select.push({"url":url,"id":id,"title":title});
            });
            data.slist=select;
            $('#shortmenuShow').append(template('shortMenuShowTem', data));//已有快捷菜单加载
            method.postd(loadurl, function(d) {
                $.each(d,function(index,item){
                    plist.push({"id":item.id,"name":item.name});
                    clist.push(item.list);
                });
                $.each(select,function(index,item){//和菜单列表比对、得到已经添加的快捷菜单id
                    var sid=item.id;
                    $.each(clist,function(index,im){
                         $.each(im,function(index,m){
                            $.each(m.tlist,function(index,n){
                                if(sid==n.id){
                                    n.flag=true;
                                }
                            });
                        });
                    });
                });
                data.firstmenu=plist;
                data.secondmenu=clist;
                $('#shortcutMenuFirst').append(template('shortcutMenuFirstTem', data));//一级菜单加载
                $('#shortcutMenuList').append(template('shortcutMenuSecondTem', data));//二三级菜单加载
            },{});
        }

        function showMoad() {
            $("#shortcutMenuBtn").click(function(event) {
                $("#showShortcutMenuModal").modal("show");
            });
        }

        function addMenu(obj) {
            var addurl = obj == undefined||obj.addUrl == undefined ? "/plus/formajax.php" : obj.addUrl,
                delurl = obj == undefined||obj.delUrl == undefined ? "/plus/formajax.php" : obj.delUrl;

            $("#shortcutMenuList a").live("click", function() {
                var $this = $(this),
                    title = $this.attr("data-title"),
                    id = $this.attr("data-id"),
                    url = $this.attr("data-url"),
                    $show = $("#shortmenuShow"),
                    $hrefList = $("#shortcutHrefList"),
                    data={"title":title,"url":url,"id":id};
                if (!$this.hasClass("show-disabled")) {
                    var html = "";
                    $show.append(template('addTem', data));
                    $hrefList.append(template('addUrlTem', data));
                    $this.find("i").attr("class", "fa fa-check m-r-xs");
                    $this.attr("title", "已添加，点击取消");
                    $this.addClass("show-disabled");
                    method.postd(addurl, function(d) {}, {
                        "id": id,
                        "url": url,
                        "title": title
                    });

                } else {
                    $show.find("a[data-id=" + id + "]").parent().remove();
                    $hrefList.find("#" + id + "").parent().remove();
                    $this.find("i").attr("class", "fa fa-plus m-r-xs");
                    $this.attr("title", "点击添加快捷菜单");
                    $this.removeClass("show-disabled");
                    method.postd(delurl, function(d) {}, {
                        "id": id,
                        "url": url,
                        "title": title
                    });
                }
            });
            $("#shortmenuShow a i").live("click", function() {
                var $a = $(this).parent(),
                    id = $a.attr("data-id"),
                    title = $a.attr("data-title"),
                    url = $a.attr("data-url"),
                    $list = $("#shortcutMenuList"),
                    $hrefList = $("#shortcutHrefList");;
                $a.parent().remove();
                $list.find("a[data-id=" + id + "]").attr({
                    "title": "点击添加快捷菜单"
                }).removeClass('show-disabled');
                $list.find("a[data-id=" + id + "]").find("i").attr("class", "fa fa-plus m-r-xs");
                $hrefList.find("#" + id + "").parent().remove();
                method.postd(delurl, function(d) {}, {
                    "id": id,
                    "url": url,
                    "title": title
                });
            });
        }
        return {
            init: function(obj) {
                loadMenu(obj);
                showMoad();
                addMenu(obj);
            }
        }
    }();

    return shortcutMenu;

});
